<template>
  <div>
    <!--卡片  -->
    <el-card>
      <!-- 表单 -->
      <el-form  label-width="80px">
        <el-row>
          <el-col :span="6" >
            <el-form-item label="关键字">
             <el-input v-model="searchOption.keyword" placeholder="根据编号搜索"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="1">
            <el-button @click="clearOption">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 记录 -->
      <el-alert
        type="info"
        :title="`共${searchRes.counts}条记录`"
        show-icon>
      </el-alert>
      <!-- 列表 -->
      <el-table
      :data="searchRes.items"
      style="width: 100%"
      :row-style="{height:'40px'}">
      <el-table-column
        prop="id"
        label="编号"
        width="240">
      </el-table-column>
      <el-table-column
        label="题型"
        width="100">
        <template slot-scope="{row}" >
          {{row.questionType==='1'?'单选':row.questionType==='2'?'多选':'简答'}}
        </template>
      </el-table-column>
      <el-table-column
        label="题目编号"
        width="360"
        >
        <template slot-scope="{row}" >
          <div :style="{color:'#1a9eff'}">
          {{row.questionIDs.map(item=>item.number).join(',')}}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="addTime"
        label="录入时间"
        width="200">
      </el-table-column>
      <el-table-column
        prop="totalSeconds"
        label="答题时间"
        width="240">
      </el-table-column>
      <el-table-column
        prop="accuracyRate"
        label="正确率"
        width="240">
      </el-table-column>
      <el-table-column
        prop="userName"
        label="录入人"
        width="200">
      </el-table-column>
      <el-table-column
        prop="tags"
        label="操作">
        <template slot-scope="{row}">
          <el-button plain type="danger" icon="el-icon-delete" @click="handleDelete(row)" circle></el-button>
      </template>
      </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="bottomTool">
       <PageTool :total='searchRes.counts' :paginationPage='searchOption.page' :paginationPagesize='searchOption.pagesize' @pageChange='pageChange' @pageSizeChange='pageSizeChange'>
       </PageTool>
      </div>
    </el-card>
    </div>
</template>

<script>
import moment from 'moment'
import PageTool from '@/module-manage/components/page-tool'
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
export default {
  data () {
    return {
      searchOption: {
        keyword: '',
        pagesize: 10,
        page: 1
      },
      searchRes: []
    }
  },
  components: { PageTool },
  async created () {
    this.getSearchOption()
  },
  methods: {
    async getSearchOption () {
      const { data } = await randoms(this.searchOption)
      data.items.forEach(item => { item.addTime = moment(item.addTime).format('YYYY-MM-DD HH:mm:ss') })
      this.searchRes = data
    },
    // 清除
    clearOption () {
      this.searchOption = {
        keyword: '',
        pagesize: 10,
        page: 1
      }
      this.getSearchOption()
    },
    // 搜索
    search () {
      this.getSearchOption()
    },
    pageChange (id) {
      this.searchOption.page = id
      this.getSearchOption()
    },
    pageSizeChange (id) {
      this.searchOption.pagesize = id
      this.getSearchOption()
    },
    // 删除
    handleDelete (row) {
      this.$confirm('此操作将删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms({ id: row.id })
        this.$message({
          type: 'success',
          message: '成功!'
        })
        this.getSearchOption()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }

  }
}
</script>

<style scoped lang='less'>
  .bottomTool{
    margin: 20px 0;
    float: right;
  }
</style>
